﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;
using TiaoTaoHelper.Wtm.Units.EFCore;

#nullable disable

namespace HengTong.DataAccess.Migrations
{
    /// <inheritdoc />
    public partial class 新增需求池 : TransactionalMigration
    {
        /// <inheritdoc />
        protected override void UpWithTransaction(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "PurchaseMethod",
                table: "SysPurchaseRequestBill");

            migrationBuilder.CreateTable(
                name: "SysPurchaseRequirementPool",
                columns: table => new
                {
                    ID = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Code = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    TotalAmount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    UrgentCount = table.Column<int>(type: "int", nullable: false),
                    State = table.Column<int>(type: "int", nullable: false),
                    IsDefault = table.Column<bool>(type: "bit", nullable: false),
                    PurchaserInChargeId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    Remark = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    CreateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    CreateBy = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    UpdateBy = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SysPurchaseRequirementPool", x => x.ID);
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPool_FrameworkUsers_PurchaserInChargeId",
                        column: x => x.PurchaserInChargeId,
                        principalTable: "FrameworkUsers",
                        principalColumn: "ID");
                });

            migrationBuilder.CreateTable(
                name: "SysPurchaseRequirementPoolEntry",
                columns: table => new
                {
                    ID = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PoolId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    RequestBillId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    RequestBillEntryId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    MaterialId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    MaterialSkuId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    RequestQuantity = table.Column<decimal>(type: "decimal(18,6)", nullable: false),
                    EstimatedPrice = table.Column<decimal>(type: "decimal(18,6)", nullable: false),
                    BudgetAmount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    PurchasedQuantity = table.Column<decimal>(type: "decimal(18,6)", nullable: true),
                    RemainingQuantity = table.Column<decimal>(type: "decimal(18,6)", nullable: false),
                    GroupId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    RequestBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    PurchaseType = table.Column<int>(type: "int", nullable: false),
                    MaterialType = table.Column<int>(type: "int", nullable: false),
                    UrgencyLevel = table.Column<int>(type: "int", nullable: true),
                    RequiredDeliveryDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                    ApprovalDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                    EntryDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                    PurchaseState = table.Column<int>(type: "int", nullable: false),
                    PurchaseStrategy = table.Column<int>(type: "int", nullable: true),
                    CreateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    CreateBy = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    UpdateBy = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SysPurchaseRequirementPoolEntry", x => x.ID);
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolEntry_FrameworkGroups_GroupId",
                        column: x => x.GroupId,
                        principalTable: "FrameworkGroups",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolEntry_SysMaterialSku_MaterialSkuId",
                        column: x => x.MaterialSkuId,
                        principalTable: "SysMaterialSku",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolEntry_SysMaterial_MaterialId",
                        column: x => x.MaterialId,
                        principalTable: "SysMaterial",
                        principalColumn: "ID",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolEntry_SysPurchaseRequestBillEntry_RequestBillEntryId",
                        column: x => x.RequestBillEntryId,
                        principalTable: "SysPurchaseRequestBillEntry",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolEntry_SysPurchaseRequestBill_RequestBillId",
                        column: x => x.RequestBillId,
                        principalTable: "SysPurchaseRequestBill",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolEntry_SysPurchaseRequirementPool_PoolId",
                        column: x => x.PoolId,
                        principalTable: "SysPurchaseRequirementPool",
                        principalColumn: "ID");
                });

            migrationBuilder.CreateTable(
                name: "SysPurchaseRequirementPoolMaterialGroup",
                columns: table => new
                {
                    ID = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PurchaseRequirementPoolId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    GroupId = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SysPurchaseRequirementPoolMaterialGroup", x => x.ID);
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolMaterialGroup_SysMaterialGroup_GroupId",
                        column: x => x.GroupId,
                        principalTable: "SysMaterialGroup",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_SysPurchaseRequirementPoolMaterialGroup_SysPurchaseRequirementPool_PurchaseRequirementPoolId",
                        column: x => x.PurchaseRequirementPoolId,
                        principalTable: "SysPurchaseRequirementPool",
                        principalColumn: "ID");
                });

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPool_PurchaserInChargeId",
                table: "SysPurchaseRequirementPool",
                column: "PurchaserInChargeId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolEntry_GroupId",
                table: "SysPurchaseRequirementPoolEntry",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolEntry_MaterialId",
                table: "SysPurchaseRequirementPoolEntry",
                column: "MaterialId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolEntry_MaterialSkuId",
                table: "SysPurchaseRequirementPoolEntry",
                column: "MaterialSkuId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolEntry_PoolId",
                table: "SysPurchaseRequirementPoolEntry",
                column: "PoolId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolEntry_RequestBillEntryId",
                table: "SysPurchaseRequirementPoolEntry",
                column: "RequestBillEntryId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolEntry_RequestBillId",
                table: "SysPurchaseRequirementPoolEntry",
                column: "RequestBillId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolMaterialGroup_GroupId",
                table: "SysPurchaseRequirementPoolMaterialGroup",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_SysPurchaseRequirementPoolMaterialGroup_PurchaseRequirementPoolId",
                table: "SysPurchaseRequirementPoolMaterialGroup",
                column: "PurchaseRequirementPoolId");
        }

        /// <inheritdoc />
        protected override void DownWithTransaction(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "SysPurchaseRequirementPoolEntry");

            migrationBuilder.DropTable(
                name: "SysPurchaseRequirementPoolMaterialGroup");

            migrationBuilder.DropTable(
                name: "SysPurchaseRequirementPool");

            migrationBuilder.AddColumn<int>(
                name: "PurchaseMethod",
                table: "SysPurchaseRequestBill",
                type: "int",
                nullable: true);
        }
    }
}
